package com.isale.model.enums;

import com.isale.common.model.view.ComboBox;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bai on 2016/11/27.
 */
public enum GoodsFromTypeEnum {
    TYPE0(0, "淘宝"),
    TYPE1(1, "天猫"),
    TYPE3(2, "京东");
    private Integer code;
    private String description;

    /**
     * @param code        数字编码
     * @param description 中文描述
     */
    private GoodsFromTypeEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }


    /**
     * 获取枚举类型数值编码
     */
    public Integer toCode() {
        return this.code == null ? this.ordinal() : this.code;
    }


    /**
     * 获取枚举类型中文描述
     */
    public String toDescription() {
        return this.description;
    }

    public String toString() {
        return toDescription();
    }


    /**
     * 按数值获取对应的枚举类型
     *
     * @param code 数值
     * @return 枚举类型
     */
    public static GoodsFromTypeEnum enumValueOf(Integer code) {
        GoodsFromTypeEnum[] values = GoodsFromTypeEnum.values();
        GoodsFromTypeEnum v = null;
        for (int i = 0; i < values.length; i++) {
            if (code.equals(values[i].toCode())) {
                v = values[i];
                break;
            }
        }
        return v;
    }


    /**
     * 获取枚举类型的所有<数字编码,中文描述>对
     *
     * @return
     */
    public static List<ComboBox> toComboBoxList() {
        List<ComboBox> list = new ArrayList<ComboBox>();
        for (int i = 0; i < GoodsFromTypeEnum.values().length; i++) {
            String text = GoodsFromTypeEnum.values()[i].toDescription();
            String value = GoodsFromTypeEnum.values()[i].toCode() + "";

            ComboBox comboBox = new ComboBox(text, value);
            list.add(comboBox);
        }
        return list;
    }
}
